package backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/25 18:38
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
 * 电话号码的字母组合
 */
public class LetterCombinations {

    static List<String> result;
    static StringBuilder s;

    public static List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return null;
        }
        s = new StringBuilder();
        result = new ArrayList<>();
        String[] nums = {"", "","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        backTracking(digits, nums, 0);
        return result;
    }


    /**
     * 比如digits如果为"23", num 为0，则str表示2对应的 abc
     */
    public static void backTracking(String digits, String[] numString, int index) {
        if (index == digits.length()) {
            result.add(s.toString());
            return;
        }
        String str = numString[digits.charAt(index) - '0'];
        for (int i = 0; i < str.length(); i++) {
            s.append(str.charAt(i));
            backTracking(digits, numString, index + 1);
            s.deleteCharAt(s.length() - 1);
        }
    }

    public static void main(String[] args) {

    }
}
